import { v4 } from 'uuid'
import { EventSourcePolyfill } from 'event-source-polyfill'
import { basePath, ai } from './config'

export const translate = {
  // 接收翻译流输出
  sseApi: `/${ai}/common/translator/createSse`,
  createSse () {
    const config = useRuntimeConfig()
    const uid = v4()
    return {
      uid,
      eventSource: new EventSourcePolyfill(config.app.baseURL + this.sseApi, {
        headers: {
          uid
        }
      })
    }
  },
  // 发送翻译请求
  translateApi: '/common/translator/translate',
  translate (body: any, uid: string) {
    return $fetch(this.translateApi, {
      method: 'post',
      body,
      headers: {
        uid
      },
      baseURL: ai
    })
  },
  // 获取语言列表
  getLanguageApi: '/v1/language',
  getLanguage () {
    return $fetch(this.getLanguageApi, {
      method: 'get',
      baseURL: basePath
    })
  }
}
